How to start e-mail programming in Java
- Details
- Written by Nam Ha Minh
- Last Updated on 09 July 2019   |   Print Email
You are a Java developer and would like to integrate e-mail functionality into your application, but you haven’t written anything related to e-mail stuff before. So here is a good place for getting started with e-mail programming in Java.
Like any other Java technologies, we need to pick up a library and understand its API before writing and testing some real code. Although the JRE/JDK does not include a built-in API for e-mail, Oracle provides an optional package called JavaMail which is probably the most popular framework for e-mail programming with Java technology. The JavaMail is also a part of JavaEE’s technology stack.
1. Official resources for JavaMail
Here are some official resources provided by Oracle for its JavaMail technology:
To begin, download the latest distribution of JavaMail (which is JavaMail 1.6.2 at the time of writing this article). A JavaMail distribution comes with the following items:
- License and copyright notices.
- Release changes.
- API documentation.
- Demo Java source code.
- Jar files for protocol-dependent implementation: imap.jar, pop3.jar, smtp.jar…
- Binary of the JavaMail API included in mail.jar file. This jar file is a combination of all jar files above. So to use JavaMail, only the mail.jar file is required.
2. Using JavaMail library
To compile and run code that is using JavaMail, add the mail.jar file to compile classpath and runtime classpath.
If you are using Java 5 or earlier, you have to download the JavaBeans Activation Framework (JAF) and add activation.jar file to the classpath. The JAF is only included in JDK since Java 6.
If you use Maven, simply specify the following dependency for your project:
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
3. Understanding of e-mail protocols: SMTP, IMAP and POP3
Because JavaMail is based on standard e-mail protocols like SMTP, IMAP, and POP3, it’s recommended to have basic understanding of how these protocols work, as well as advantages and disadvantages of each protocol. That would help you implement your code more efficiently. Here are the links for these protocols on Wikipedia:
- Simple Mail Transfer Protocol.
- Internet Message Access Protocol.
And finally, once you have grasped the stuff above, follow our series of tutorial on JavaMail technology:
- Send e-mail in plain text using JavaMail
- Send e-mail in HTML format using JavaMail API
- Send e-mail with attachment in Java
- Receive e-mail messages from a POP3-IMAP server
- Download attachments in e-mail messages using JavaMail
- Delete e-mail messages on mail server programmatically
- Using JavaMail for searching e-mail messages
- Embedding images into e-mail with JavaMail
Comments
i want to submit contact form only what is the code for send only contact form
please help me